package com.android.delivery.wxapi;

import android.app.Activity;
import android.content.Intent;

import com.android.delivery.constant.Constants;
import com.android.delivery.entity.even.WxPayEvent;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信支付结果返回码
 *
 * @author liyu
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    public static final String KEY_WXPAY_CODE = "wxpay_code";
    public static final String KEY_WXPAY_MSG = "wxpay_msg";
    private IWXAPI api;

    @Override
    public void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            String payStr = resp.errStr;
            int code = resp.errCode;
            EventBus.getDefault().post(new WxPayEvent(code, payStr));
        }
        finish();
    }
}
